import requests
import time

class Pamp3:
    # urllist = []
    # closelist = []
    # s = None
    # header = {}

    def __init__(self):
        self.s = requests.Session()
        self.urllist = []
        self.header = {}
        self.closelist = []
    
    def addurl(self,url):
        self.urllist.append(url)
    

    def getflielen(self,url):
        self.s.headers['Range'] = 'bytes=0-0'
        res = self.s.get(url = url)
        leninfo = res.headers['Content-Range']
        length = int(leninfo[leninfo.rfind('/')+1:])
        return length



    def download(self,url):
        filename = url[url.rfind('/')+1:]
        print('开始下载')
        print(filename,url)
        length = self.getflielen(url)
        nowlen = 0
        step = 5*1024*1024
        
        with open(filename,'wb') as f:
            while nowlen < length:
                flag = True
                while flag:              
                    # 失败重试未完成
                    try:
                        self.s.headers['Range'] = 'bytes={}-{}'.format(nowlen,nowlen+step if nowlen+step < length else length)
                        res = self.s.get(url = url,timeout = 30)
                        f.write(res.content)
                        print(nowlen,length)
                        nowlen += step+1
                        flag = False
                        # time.sleep(1)
                    except:
                        pass

        print('下载完成')

    def addheaders(self,header):
        self.header.update(header)

    def geturl(self):
        url = self.urllist.pop(0)
        self.closelist.append(url)
        return url

    def run(self):
        while self.urllist:
            url = self.geturl()
            self.download(url)





def main():
    header = {
        'Cookie':'safedog-flow-item=9C1D89B1084233C26B2BEA2A3BAF95EC; UM_distinctid=169f018b00f413-0f79b7fe781353-b78173e-1fa400-169f018b011225',
        'Host':'mp3-f.ting89.com:9090',
        'Pragma':'no-cache',
        'Referer':'http://down.ting89.com/ting89/js/play/iOS.php?id=http%3A%2F%2Fmp3%2Df%2Eting89%2Ecom%3A9090%2F2019%2F04%2F%C3%F1%BC%E4%B7%E7%CB%AE%C6%E6%CC%B7%2F0002%2Emp3&line=0&from=mp3&next=http%3A%2F%2Fm.ting89.com%2Fplaybook%2F%3F15526-0-2.html',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
    }    
    # get_url = 'http://mp3-f.ting89.com:9090/2019/04/民间风水奇谭/0002.mp3'
    get_url = 'https://dldir1.qq.com/qqfile/qq/QQ9.1.0/24712/QQ9.1.0.24712.exe'
    app = Pamp3()
    app.addurl(get_url)
    app.addheaders(header)
    app.run()


if __name__ == "__main__":
    main()

# s = requests.Session()
# header = {
#     'Cookie':'safedog-flow-item=9C1D89B1084233C26B2BEA2A3BAF95EC; UM_distinctid=169f018b00f413-0f79b7fe781353-b78173e-1fa400-169f018b011225',
# 'Host':'mp3-f.ting89.com:9090',
# 'Pragma':'no-cache',
# 'Range':'bytes=0-',
# 'Referer':'http://down.ting89.com/ting89/js/play/iOS.php?id=http%3A%2F%2Fmp3%2Df%2Eting89%2Ecom%3A9090%2F2019%2F04%2F%C3%F1%BC%E4%B7%E7%CB%AE%C6%E6%CC%B7%2F0002%2Emp3&line=0&from=mp3&next=http%3A%2F%2Fm.ting89.com%2Fplaybook%2F%3F15526-0-2.html',
# 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',

# }

# # res = s.get(url = 'http://mp3-f.ting89.com:9090/2019/04/民间风水奇谭/0002.mp3')



# def getfilelen(url):
#     global s




# for i in range(1,443):
#     name = '{:0>4}.mp3'.format(i)
#     print(name)

#     get_url = 'http://mp3-f.ting89.com:9090/2019/04/民间风水奇谭/{}'.format(name)
#     header['Range'] = 'bytes=0-0'
#     res = s.get(url = get_url)


#     with open(name,'wb') as f:
#         f.write(res.content)

#     print('OK')